package com.mo.demo.ehcache;

import lombok.extern.slf4j.Slf4j;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;

@Slf4j
public class EhcacheDemo {
    public static void main(String[] args) {

        XmlConfiguration xmlConfiguration = new XmlConfiguration(EhcacheDemo.class.getResource("/ehcache.xml"));
        CacheManager manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
        manager.init();

        Cache<String,String> cache = manager.getCache("foo",String.class,String.class);

        int count = 10;

        for (int i = 0; i < count; i++) {
          cache.put(i+"",i+""+i);
        }

        for (int i = 0; i < count; i++) {
            log.info("key={}, value={}",i,cache.get(i+""));
        }

        // 输出
        // 05:44:07.159 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=0, value=00
        // 05:44:07.159 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=1, value=11
        // 05:44:07.159 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=2, value=22
        // 05:44:07.160 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=3, value=33
        // 05:44:07.160 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=4, value=44
        // 05:44:07.160 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=5, value=55
        // 05:44:07.160 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=6, value=66
        // 05:44:07.160 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=7, value=77
        // 05:44:07.161 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=8, value=88
        // 05:44:07.161 [main] INFO com.mo.demo.ehcache.EhcacheDemo - key=9, value=9
    }
}
